#! /bin/sh
#
# smartmontools initd file for Cygwin smartd
#
# Home page of code is: http://www.smartmontools.org
#
# Copyright (C) 2004-17 Christian Franke
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# $Id$
#

SMARTD_BIN=/usr/local/sbin/smartd

# The following settings may be changed by the configuration file below
# Service Name (must be unique)
smartd_svcname=smartd
# Service display name
smartd_svcdisp="CYGWIN smartd"
# Service description
smartd_svcdesc="\
Controls and monitors storage devices using the Self-Monitoring, \
Analysis and Reporting Technology System (SMART) built into \
ATA/SATA and SCSI/SAS hard drives and solid-state drives. \
www.smartmontools.org"

# Source configuration file.
[ -r /etc/sysconfig/smartmontools ] && . /etc/sysconfig/smartmontools

PID_FILE=/var/run/smartd.pid
RETVAL=0

# Note: "[ -r $PID_FILE ]" is not used here. On Cygwin, this command may
# return success even if the file is present but cannot be read by current user.
# If smartd is running as service, smartd.pid is owned by local system account
# which is different from any user ever executing this script.

case "$1" in
  start)
    if cygrunsrv -L 2>/dev/null | grep "^${smartd_svcname}$" >/dev/null 2>&1; then
      echo -n "Starting service $smartd_svcname: "
      cygrunsrv -S "$smartd_svcname"
    else
      echo -n "Starting smartd as daemon: "
      $SMARTD_BIN -p $PID_FILE $smartd_opts
    fi
    RETVAL=$?
    ;;
  stop)
    echo -n "Shutting down smartd: "
    pid="`cat $PID_FILE 2>/dev/null`" && kill "$pid"
    RETVAL=$?
    ;;
  reload)
    echo -n "Reloading smartd configuration: "
    pid="`cat $PID_FILE 2>/dev/null`" && kill -HUP "$pid"
    RETVAL=$?
    ;;
  report)
    echo -n "Checking SMART devices now: "
    pid="`cat $PID_FILE 2>/dev/null`" && kill -USR1 "$pid"
    RETVAL=$?
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    exit $?
    ;;
  install)
    shift
    [ $# -eq 0 ] || smartd_opts="$*"
    dep=; dep2=
    if cygrunsrv -L 2>/dev/null | grep "^syslogd$" >/dev/null 2>&1; then
      dep="syslogd"
    fi
    if cygrunsrv -L 2>/dev/null | grep "^syslog-ng" >/dev/null 2>&1; then
      dep2="syslog-ng"
    fi
    if [ -z "$dep" ]; then
      if [ -z "$dep2" ]; then
        echo "Warning: no syslog service installed, smartd will write to windows event log.";
      else
        dep="$dep2"
      fi
    else
      if [ -z "$dep2" ]; then
        :
      else
        dep=
        echo "Warning: both syslogd and syslog-ng installed, dependency not set."
      fi
    fi
    echo "Installing service ${smartd_svcname}${dep:+ (depending on '$dep')}${smartd_opts:+ with options '$smartd_opts'}:"
    cygrunsrv -I "$smartd_svcname" -d "$smartd_svcdisp"  -f "$smartd_svcdesc" ${dep:+-y} $dep \
              -e CYGWIN="$CYGWIN" -p $SMARTD_BIN -a "-n -p ${PID_FILE}${smartd_opts:+ }$smartd_opts"
    RETVAL=$?
    ;;
  remove)
    echo "Removing service $smartd_svcname:"
    cygrunsrv -R "$smartd_svcname"
    RETVAL=$?
    ;;
  status)
    echo -n "Checking smartd status: "
    if cygrunsrv -L 2>/dev/null | grep "^${smartd_svcname}$" >/dev/null 2>&1; then
      if cygrunsrv -Q "$smartd_svcname" 2>/dev/null | grep "State *: Running" >/dev/null 2>&1; then
        echo "running as service '$smartd_svcname'."
      elif ps -e 2>/dev/null | grep " ${SMARTD_BIN}$" >/dev/null 2>&1; then
        echo "installed as service '$smartd_svcname' but running as daemon."
      else
        echo "installed as service '$smartd_svcname' but not running."
        RETVAL=1
      fi
    elif ps -e 2>/dev/null | grep " ${SMARTD_BIN}$" >/dev/null 2>&1; then
      echo "running as daemon."
    else
      echo "not running."
      RETVAL=1
    fi
    exit $RETVAL
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|reload|report|status}"
    echo "       $0 {install [options]|remove}"
    exit 1
esac

if [ "$RETVAL" -eq 0 ]; then echo "done"; else echo "ERROR"; fi
exit $RETVAL
